package com.order.api.vo.response;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@ApiModel(description = "分页信息返回")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResponse<T> {

    @ApiModelProperty(value="页码",position=1)
    private int pageNum;

    @ApiModelProperty(value="每页条数",position=2)
    private int pageSize;

    @ApiModelProperty(value="总条数",position=3)
    private long total;

    @ApiModelProperty(value="总页数",position=4)
    private int pages;

    @ApiModelProperty(value="分页集合数据",position=5)
    private List<T> list;

    @ApiModelProperty(value="上一页页码",position=6)
    private int prePage;

    @ApiModelProperty(value="下一页页码",position=7)
    private int nextPage;

    public static <T> PageResponse<T> getInstance(PageInfo<T> pageInfo) {
        PageResponse response = (PageResponse<T>) PageResponse.builder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .pages(pageInfo.getPages())
                .prePage(pageInfo.getPrePage())
                .nextPage(pageInfo.getNextPage())
                .build();
        response.setList(pageInfo.getList());
        return response;
    }

    public static <T> PageResponse<T> getInstance(PageInfo pageInfo, List<T> list) {
        PageResponse response = (PageResponse<T>) PageResponse.builder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .pages(pageInfo.getPages())
                .prePage(pageInfo.getPrePage())
                .nextPage(pageInfo.getNextPage())
                .build();
        response.setList(list);
        return response;
    }
}
